// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.toobar;

import org.dengues.ui.web.URLDialog;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Pattern;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-3-6 qiang.zhang $
 * 
 */
public class AnnouncementContributionItem extends ContributionItem {

    static final int TIMER = 30;

    private ToolItem toolitem;

    private Canvas canvas;

    private String text = "Announcement";

    private int textWidth;

    private int textHeight;

    private float x;

    private float y;

    float incX = 1.0f;

    private Composite fillComposite;

    private boolean stop = false;

    /**
     * Qiang.Zhang.Adolf@gmail.com SchoolAnnouncementAction constructor comment.
     */
    public AnnouncementContributionItem(String message) {
        super("test");
        this.text = message;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createControl".
     * 
     * @param parent
     */
    protected Control createControl(Composite parent) {
        if (fillComposite == null) {
            fillComposite = new Composite(parent, SWT.BORDER);
            // text.setText("Text test!");
            GridLayout gridLayout = new GridLayout();
            gridLayout.marginHeight = 0;
            gridLayout.marginWidth = 0;

            fillComposite.setLayout(gridLayout);
            GridData gridData = new GridData(GridData.FILL_BOTH);
            fillComposite.setLayoutData(gridData);
            toolitem.setWidth(computeWidth(fillComposite));
            createCanvas(fillComposite);
            gridData = new GridData(GridData.FILL_BOTH);
            canvas.setLayoutData(gridData);
            final Display display = fillComposite.getDisplay();
            display.timerExec(TIMER, new Runnable() {

                public void run() {
                    if (canvas.isDisposed()) {
                        return;
                    }
                    int timeout = TIMER;
                    if (!stop) {
                        Rectangle rect = canvas.getClientArea();
                        next(rect.width, rect.height);
                        canvas.redraw();
                        // canvas.update();
                        // timeout = animTab.getAnimationTime();
                    }
                    display.timerExec(timeout, this);
                }
            });
        }
        return fillComposite;
    }

    protected int computeWidth(Control control) {
        return control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x + 100;
    }

    public void fill(ToolBar parent, int index) {
        toolitem = new ToolItem(parent, SWT.SEPARATOR, index);
        Control control = createControl(parent);
        toolitem.setControl(control);
    }

    private Control createCanvas(Composite parent) {
        if (canvas == null) {
            int style = SWT.NO_BACKGROUND;
            // style |= SWT.DOUBLE_BUFFERED;
            canvas = new Canvas(parent, style);
            canvas.addPaintListener(new PaintListener() {

                public void paintControl(PaintEvent event) {
                    GC gc = event.gc;
                    Rectangle rect = canvas.getClientArea();
                    Device device = gc.getDevice();
                    Pattern pattern = null;
                    Color bgColor1 = Display.getDefault().getSystemColor(SWT.COLOR_BLUE);
                    Color bgColor2 = Display.getDefault().getSystemColor(SWT.COLOR_BLUE);
                    pattern = new Pattern(device, 0, 0, rect.width, rect.height, bgColor1, bgColor2);
                    gc.setBackgroundPattern(pattern);
                    // pattern = new Pattern(device, background.getBgImage());
                    // gc.setBackgroundPattern(pattern);
                    gc.fillRectangle(rect);
                    paint(gc, rect.width, rect.height);
                    if (pattern != null) {
                        pattern.dispose();
                    }
                }
            });
            canvas.addMouseTrackListener(new MouseTrackListener() {

                public void mouseEnter(MouseEvent e) {
                    canvas.setCursor(new Cursor(canvas.getDisplay(), SWT.CURSOR_HAND));
                    stop = true;
                }

                public void mouseExit(MouseEvent e) {
                    stop = false;
                }

                public void mouseHover(MouseEvent e) {

                }

            });
            canvas.addMouseListener(new MouseAdapter() {

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.swt.events.MouseAdapter#mouseDown(org.eclipse.swt.events.MouseEvent)
                 */
                @Override
                public void mouseDown(MouseEvent e) {
                    URLDialog dialog = new URLDialog(new Shell(), "http://groups.google.com/group/dengues/");
                    dialog.open();
                }

            });
        }
        return canvas;
    }

    public void addSelectionListner(MouseListener listener) {
        canvas.addMouseListener(listener);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "paint".
     * 
     * @param gc
     * @param width
     * @param height
     */
    protected void paint(GC gc, int width, int height) {
        Device device = gc.getDevice();
        FontData fd = device.getSystemFont().getFontData()[0];
        Font font = new Font(device, fd.getName(), 12, SWT.BOLD);
        gc.setFont(font);
        Point size = gc.stringExtent(text);
        textWidth = size.x;
        textHeight = size.y;

        // Path path = new Path(device);
        // path.addString(text, x, y, font);
        // gc.setClipping(path);
        gc.setClipping((Rectangle) null);
        gc.setForeground(device.getSystemColor(SWT.COLOR_RED));
        // gc.drawPath(path);
        // path.dispose();
        gc.drawString(text, (int) x, (int) y);
    }

    public void next(int width, int height) {
        x -= incX;
        if (Math.abs(x) > width) {
            x = width;
        }
    }
}
